<template>
  <div class="app-container">
    <div class="header-square">
      <el-button type="primary" size="small" @click="openAdd">新增</el-button>
    </div>
    <el-table :data="list" v-loading.body="listLoading" element-loading-text="Loading" border size="small">
      <el-table-column align="center" label='名称' width="150">
        <template slot-scope="scope">
          {{scope.row.name}}
        </template>
      </el-table-column>
      <el-table-column label="年份" align="center" width="60">
        <template slot-scope="scope">
          {{scope.row.year}}
        </template>
      </el-table-column>
      <el-table-column label="酒庄" width="" align="center">
        <template slot-scope="scope">
          <span>{{scope.row.maker}}</span>
        </template>
      </el-table-column>
      <el-table-column label="产地" width="" align="center">
        <template slot-scope="scope">
          {{scope.row.procuctionLocation}}
        </template>
      </el-table-column>
      <el-table-column label="酒精浓度" width="70" align="center">
        <template slot-scope="scope">
          {{scope.row.alcholoPercentage}}%
        </template>
      </el-table-column>
      <el-table-column label="生产日期" width="" align="center">
        <template slot-scope="scope">
          {{scope.row.makeTime}}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     v-on:click="openDetail(scope.row.id)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--编辑弹窗-->
    <template>
      <edit-page ref="detailCompoent"></edit-page>
    </template>
    <!--编辑弹窗end-->
    <!--新增弹窗start-->
    <template>
      <add-page ref="addCompoent" v-on:updateData="fetchData"></add-page>
    </template>
    <!--新增弹窗end-->
  </div>
</template>

<script>
  import {getList} from '@/api/product'
  import editPage from './components/editPage'
  import addPage from './components/addPage'

  export default {
    components:{
      editPage:editPage,
      addPage:addPage,
    },
    data() {
      return {
        list: null,
        listLoading: true,
      }
    },
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'gray',
          deleted: 'danger'
        }
        return statusMap[status]
      }
    },
    created() {
      this.fetchData();
    },
    methods: {
      //初始化表格数据
      fetchData() {
        this.listLoading = true;
        getList().then(response => {
          if (response.code === 0) {
            this.list = response.data;
          }
          this.listLoading = false;
        })
      },
      //打开详情弹窗
      openDetail(id) {
        this.$refs.addCompoent.openAdd(2,id);
      },
      /**
       * 打开新增
       */
      openAdd(){
        this.$refs.addCompoent.openAdd(1);
      }
    }
  }
</script>
<style>
  .el-dialog__header{
    background-color:#f5f5f5;
    padding:10px;
  }
  .el-dialog__title{
    font-size:16px;
  }
  .el-dialog__body{
    padding:10px;
  }
  .el-dialog__headerbtn{
    top:14px;
  }
</style>
